package com.gk.company.view.bottom
{
	import com.gk.app.GT;
	import com.gk.app.Language;
	import com.gk.company.view.bottom.item.FriendItemView;
	import com.gk.init.model.vo.UserInfoVo;
	import com.gk.util.GameSprite;
	import com.gk.util.component.HBox;
	import com.gk.util.component.InputText;
	import com.gk.util.component.LabelButton;
	import com.gk.util.component.Panel;
	import com.gk.util.component.ScrollBar;
	import com.gk.util.component.ScrollPanel;
	import com.gk.util.component.pageupdown.PageUpDown;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	public class FriendStaffView extends GameSprite
	{
		private var friendBtn:LabelButton;
		private var staffBtn:LabelButton;
		private var infoBox:HBox;
		private var upBtn:LabelButton;
		private var downBtn:LabelButton;
		private var allUpBtn:LabelButton;
		private var allDownBtn:LabelButton;
		private var pageUpDown:PageUpDown;
		private var friendVect:Vector.<UserInfoVo>;
		private var staffVect:Vector.<UserInfoVo>;
		private var isFriend:Boolean;//当前是否显示好友信息
		
		private const prePage:int = 5;//每页显示个数
		
		private var searchInfo:InputText;

		private var inviteFriend:LabelButton;

		private var recruitBtn:LabelButton;
		
		
		public function FriendStaffView(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			
			this.friendVect = new Vector.<UserInfoVo>();
			this.staffVect = new Vector.<UserInfoVo>();
			
			
			this.friendBtn = new LabelButton(this,10,20,Language.COMPANY_SYS_BTN[0],friendBtnCkHandler);
			this.friendBtn.setSize(50,20);
			this.staffBtn = new LabelButton(this,10,50,Language.COMPANY_SYS_BTN[1],staffBtnCkHandler);
			this.staffBtn.setSize(50,20);
			this.friendBtn.toggle = this.staffBtn.toggle = true;
			friendBtn.selected = true;
			
			this.isFriend = true;
			
			var panel:Panel = new Panel(this,80,0);
			panel.setSize(660,120);
			
			infoBox = new HBox(this,125,10);
			infoBox.spacing = 20;
			
			this.upBtn = new LabelButton(this,90,30,"上页");
			this.upBtn.setSize(30,20);
			this.downBtn = new LabelButton(this,610,30,"下页");
			this.downBtn.setSize(30,20);
			this.allUpBtn = new LabelButton(this,90,60,"首页");
			this.allUpBtn.setSize(30,20);
			this.allDownBtn = new LabelButton(this,610,60,"尾页");
			this.allDownBtn.setSize(30,20);
			this.pageUpDown = new PageUpDown(upBtn,downBtn,changeData);
			this.pageUpDown.setAllUpDwonBtn(allUpBtn,allDownBtn);
			
			inviteFriend = new LabelButton(this,650,10,"邀请好友");
			inviteFriend.setSize(80,100);
			recruitBtn = new LabelButton(this,650,10,"招聘员工");
			recruitBtn.setSize(80,100);
			inviteFriend.visible = isFriend;
			recruitBtn.visible = !isFriend;
			
			searchInfo = new InputText(this,650,-30,"搜索",inputFrinedChange);
			searchInfo.setSize(80,20);
			searchInfo.textField.addEventListener(FocusEvent.FOCUS_IN,searchInHandler);
			searchInfo.textField.addEventListener(FocusEvent.FOCUS_OUT,searchOutHandler);
		}
		
		protected function searchOutHandler(event:FocusEvent):void
		{
			searchInfo.text = "搜索";
		}
		
		protected function searchInHandler(event:FocusEvent):void
		{
			searchInfo.text = "";
		}
		
		private function inputFrinedChange(e:Event):void
		{
			var tmpName:String = searchInfo.text;
			var vect:Vector.<UserInfoVo> = isFriend?friendVect:staffVect;
			
		}
		
		private function changeData(crtPage:int):void
		{
			var vect:Vector.<UserInfoVo> = isFriend?friendVect:staffVect;
			var startIndex:int = (crtPage-1) * prePage;
			var endIndex:int = crtPage * prePage > vect.length?vect.length:crtPage * prePage;
			GT.clearChildren(infoBox);
			for (var i:int = startIndex ; i < endIndex; i++) 
			{
				new FriendItemView(vect[i],infoBox);
			}
		}
		
		//+++++++++++++++++ eventHandler  +++++++++++++++++
		
		private function staffBtnCkHandler(e:Event):void
		{
			if(isFriend){
				friendBtn.selected = false;
				staffBtn.selected = true;
				isFriend = false;
				inviteFriend.visible = isFriend;
				recruitBtn.visible = !isFriend;
				pageUpDown.maxPage = Math.ceil(staffVect.length/prePage);
				changeData(1);
			}
		}
		
		private function friendBtnCkHandler(e:Event):void
		{
			if(!isFriend){
				staffBtn.selected = false;
				friendBtn.selected = true;
				this.isFriend = true;
				inviteFriend.visible = isFriend;
				recruitBtn.visible = !isFriend;
				pageUpDown.maxPage = Math.ceil(friendVect.length/prePage);
				changeData(1);
			}
		}
		
		
		
		//++++++++++++++ public mothed +++++++++++++++++++
		/**
		 *更新好友信息 
		 * @param vect
		 * 
		 */		
		public function updateFriendInfo(vect:Vector.<UserInfoVo>):void{
			this.friendVect = vect;
			
			for(var i:int=0;i<10;i++){
				this.friendVect.push(vect[int(vect.length * Math.random())].clone());
			}
			
			if(isFriend){
				pageUpDown.maxPage = Math.ceil(vect.length/prePage);
				changeData(1);
			}
		}
		
		/**
		 *更新员工信息 
		 * @param vect
		 * 
		 */		
		public function updateStaffInfo(vect:Vector.<UserInfoVo>):void{
			this.staffVect = vect;
			if(!isFriend){
				pageUpDown.maxPage = Math.ceil(vect.length/prePage);
				changeData(1);
			}
		}
		
		
		
	}
}